// pages/shoping/shoping.js
import {getSetting,chooseAddress,openSetting,showModal,showToast} from "../../utils/sync.js";
// async
import regeneratorRuntime from '../../utils/regenerator-runtime/runtime.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
    cart:[],
    allchecked:false,
    totalpirce:0,
    totalnum:0
  },
onShow:function(){
  var cart = wx.getStorageSync("cart") || [];
   let address = wx.getStorageSync('address');
  //  const allchecked = cart.length?cart.every(v=>v.checked):false;
  this.setData({
    address
  })
this.publicCart(cart);
},
// 收货地址
async shopingSite(){
  /* 获取收货地址*/
    // wx.getSetting({
    //   success (res) {
    //     console.log(res)
    //     const scopeAddress = res.authSetting["scope.address"];
    //     console.log(scopeAddress)
    //     if(scopeAddress === true||scopeAddress === undefined){
    //       wx.choonseAddress({
    //             success (res1) {
    //               console.log(res1)
    //             }
    //           })
    //     }else{
    //       wx.openSetting({
    //         success (res2) {
    //           console.log(res2);
    //           wx.chooseAddress({
    //             success (res3) {
    //               console.log(res3)
    //             }
    //           })
    //         }
    //       })
    //     }
    //   }
    // })
    /*改良收货地址*/
    try {
      let res = await wx.getSetting();
    const scopeAddress = res.authSetting["scope.address"];
  if(scopeAddress === false){
      let res1 =  await openSetting();
  }
  let address = await chooseAddress();
  wx.setStorageSync('address', address)
    } catch (error) {
      console.log(error)
    }
  },
// 商品选中
checked(e){
 console.log(e)
//  获取商品id
const goods_id = e.currentTarget.dataset.id;
// 获取商品数组
let {cart} = this.data;
//  获取需要修改的商品对象
 let index = cart.findIndex(v=>v.goods_id === goods_id);
 console.log(index)
//  取反
 cart[index].checked =!cart[index].checked
  this.publicCart(cart);
},
// 公共商品总价格、总数量
publicCart(cart){
let allchecked =true;
//  商品总价格、总数量
let totalpirce = 0;
let totalnum = 0;
cart.forEach(v=>{
  if(v.checked){
  totalpirce +=v.num*v.goods_price
  totalnum +=v.num
 //  console.log(v)
 }else{
     allchecked = false
 }
})
allchecked = cart.length !=0?allchecked:false;
 this.setData({
   cart,
   allchecked,
   totalpirce,
   totalnum
 })
 //  把修改的商品对象存入缓存
wx.setStorageSync('cart', cart);
},
// 全选
checkAll(){
  // 获取data的商品数组、全选allchecked
  let {cart,allchecked} = this.data;
  console.log(allchecked)
  // 反向
  allchecked =!allchecked;
  // 循环遍历商品数组
  cart.forEach(v => v.checked=allchecked);
  // 调取公共函数
  this.publicCart(cart)
},
// 商品加减
async addEnd(e){
  // 获取商品对象的id
 const {paration,id} = e.currentTarget.dataset;
//  获取商品数组对象
let {cart} = this.data;
console.log(cart)
// 获取要修改的商品索引
let index = cart.findIndex(v=>v.goods_id===id);
console.log(index)
// 判断商品数量num为1时paration为-1时提示删除商品
if(cart[index].num === 1&&paration===-1){
  const res = await showModal({content:"您确定要删除这件商品？"});
  if(res.confirm){
    cart.splice(index,1);
    this.publicCart(cart);
  }
}else{
// 修改点击的商品数量
cart[index].num+=paration;
// 将数据重新添加到缓存中
this.publicCart(cart)
}

},
// 结算
async pay(){
   let {address,totalnum} = this.data;
  //  console.log(address,totalnum);
   if(address === ""){
      let res = await showToast({title:"您还没有填写收货地址"});
   }else if(totalnum==""){
    let res = await showToast({title:"您还没有选择商品"});
   }else{
     let {cart} = this.data;
     let cartChecked = cart.filter(v=>v.checked);
     wx.setStorageSync('cartChecked', cartChecked)
     console.log(cartChecked)
    wx.navigateTo({
      url: '/pages/pay/pay'
    })
   }
}
}) 